<?php
// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | Author: create by allen.qiang
// +----------------------------------------------------------------------

/**
 * 商品分类操作功能
 *
 * @package 	htdocs
 * @subpackage  quanliadmin
 * @author  	create by allen.qiang
 */ 

quanli_require('module/base/goods/goodsCategory.class.php');

class pa_category {

    public function execute() {
        global $G_SHOP ;
       
        if (isset($_REQUEST['operFlg'])) {

            switch($_REQUEST['operFlg']) {

                case 1: //商品分类列表
                    $this->categoryList();
                    break;
                case 2: //商品分类添加
                    $this->categoryAdd();
                    break;
                case 3: //商品分类编辑
                    $this->categoryEdit();
                    break;
                default:
                    $G_SHOP->smarty->assign('error_msg', '参数错误，请审核后再操作');
                    $G_SHOP->smarty->display('comm/error.tpl');
            }
        }
    }

    /**
     * 商品分类列表
     * @author sunchun
     *
     */
    public function categoryList() {
    	
    	global $G_SHOP;
       
    	$category = $this->getCategory();

        //商品分类编辑权限
        $G_SHOP->smarty->assign('edit_permission', $G_SHOP->has_permission(502));
        
        $G_SHOP->smarty->assign_by_ref('category', $category);
        
        $G_SHOP->smarty->display('goods/categoryList.tpl');
    }

    /**
     * 商品分类添加
     *
     */
    public function categoryAdd() {
    	
        global $G_SHOP;
        
        $parentID = isset($_REQUEST['parentID']) ? trim($_REQUEST['parentID']) : null;
        
        //获取分类信息
        $category = $this->getCategory();

        //规格组
        $specGroup = $G_SHOP->DBCA->stmt_query('SELECT * FROM goods_spec_group WHERE useFlg = 1');  

        //标签组
        $tagGroup = $G_SHOP->DBCA->stmt_query('SELECT * FROM goods_tag_group WHERE useFlg = 1');
        
        $G_SHOP->smarty->assign('spec_group',$specGroup);
        $G_SHOP->smarty->assign('tag_group' , $tagGroup);
        $G_SHOP->smarty->assign('parentID' , $parentID);
        $G_SHOP->smarty->assign('category' , $category);
        
        $G_SHOP->smarty->display('goods/categoryAdd.tpl');
    }

    /**
     * 商品分类编辑
     *
     */
    public function categoryEdit() {
        global $G_SHOP;

        $categoryID = isset($_REQUEST['categoryID']) ? trim($_REQUEST['categoryID']) : null;
        $level		= isset($_REQUEST['levelID']) ? trim($_REQUEST['levelID']) : null;
        
        switch ($level) {
        	case 1:	//大分类
        		$categoryInfo = new entity('goods_bigcategory');
        		$categoryInfo->bigCategoryID = $categoryID;
        		
        	break;
        	case 2:	//中分类
        		$categoryInfo = new entity('goods_midcategory');
        		$categoryInfo->midCategoryID = $categoryID;
        		
        	break;
        	case 3:	//小分类
        		$categoryInfo = new entity('goods_smallcategory');
        		$categoryInfo->smallCategoryID = $categoryID;
        		
        	break;
        	default:
        		$G_SHOP->smarty->display('comm/error.tpl');
        		die();
        	break;	
        }
        
        if (!$G_SHOP->DBCA->fetch($categoryInfo)) {
        	$G_SHOP->smarty->display('comm/error.tpl');
        	die();
        }
        
         switch ($level) {
         	case 1:	//大分类
        		$categoryStr = $categoryInfo->bigCategoryID;
        		$categoryInfo->id = $categoryID . '_' . 1;
        		$categoryInfo->parentID = '0_0_1';
        	break;
        	case 2:	//中分类
        		$categoryStr = $categoryInfo->bigCategoryID . $categoryInfo->midCategoryID;
        		$categoryInfo->id = $categoryID . '_' . 2;
        		$categoryInfo->parentID = $categoryInfo->bigCategoryID . '_0_2';
        	break;
        	case 3:	//小分类
        		$categoryStr = $categoryInfo->bigCategoryID . $categoryInfo->midCategoryID . $categoryInfo->smallCategoryID;
        		$categoryInfo->id = $categoryID . '_' . 3;
        		$categoryInfo->parentID = $categoryInfo->bigCategoryID . '_' . $categoryInfo->midCategoryID . '_3';
        	break;
         }

        //获取商品标签组
        $tag_group = $G_SHOP->DBCA->stmt_query('SELECT * FROM goods_tag_group WHERE useFlg = 1');

        //获取已选标签组
        $category_tag = $G_SHOP->DBCA->stmt_query(sprintf('SELECT * FROM goods_category_tagGroup WHERE categoryID = %s', $categoryStr));

        //获取商品规格组
        $spec_group = $G_SHOP->DBCA->stmt_query('SELECT * FROM goods_spec_group WHERE useFlg = 1');
        
        //获取已选规格组
        $category_spec = $G_SHOP->DBCA->stmt_query(sprintf('SELECT * FROM goods_category_specGroup WHERE categoryID = %s',$categoryStr));
        
        //获取分类信息
        $category = $this->getCategory();
        
        $G_SHOP->smarty->assign('categoryInfo' , $categoryInfo);
        $G_SHOP->smarty->assign('tag_group' , $tag_group);
        $G_SHOP->smarty->assign('category_tag' , $category_tag);
        $G_SHOP->smarty->assign('spec_group',$spec_group);
        $G_SHOP->smarty->assign('category_spec',$category_spec);
        $G_SHOP->smarty->assign('category' , $category);
 
        $G_SHOP->smarty->display('goods/categoryAdd.tpl');
    }
    
    /**
     * 获取所有分类信息
     *
     */
   	public function getCategory(){
   		
   		global $G_SHOP;

        $categoryHandler = new goodsCategory( $G_SHOP->DBCA ) ;
        
        //查询大分类
        $big_array = array(
        	'sortname' => 'sortOrder', 
        	'sortorder' => 'ASC'
        );
        $category = $categoryHandler->searchBigCategory( $big_array ) ;
        
        //查询中分类
        if ($category) {
        	foreach ($category as $b=>$bigVal) {
        		$mid_array = array(
        			'bigCategoryID' => $bigVal['bigCategoryID'],
        			'sortname'      => 'sortOrder', 
        			'sortorder'     => 'ASC'
        		);
        		$mid_category = $categoryHandler->searchMidCategory( $mid_array ) ;
        		
        		if ($mid_category) {
        			foreach ($mid_category as $m=>$midVal) {
        				$small_array = array(
        					'bigCategoryID' => $bigVal['bigCategoryID'],
        					'midCategoryID' => $midVal['midCategoryID'],
		        			'sortname'      => 'sortOrder', 
		        			'sortorder'     => 'ASC'
        				);
        				$small_category = $categoryHandler->searchSmallCategory( $small_array ) ;
        				
        				$mid_category[$m]['smallCategory'] = $small_category;
        			}
        		}
        		$category[$b]['midCategory'] = $mid_category;
        	}
        }
        
        return $category;
   	}
}